import Vue from 'vue'
import Vuex from 'vuex'
import {getRouter, getUserInfo} from "@/api";

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        userInfo: {},
        userMenus: [],
        permissions:[],
    },
    getters: {
        userInfo: state => state.userInfo,
        userMenus: state => state.userMenus,
        permissions: state => state.permissions,
    },
    mutations: {
        SET_USERINFO(state, userInfo) {
            state.userInfo = userInfo
        },
        SET_PERMISSIONS(state, permissions) {
            state.permissions = permissions
        },
        SET_MENUS(state, userMenus) {
            state.userMenus = userMenus
        },
    },
    actions: {
        LogOut({commit}) {
            commit('SET_USERINFO', {})
            commit('SET_MENUS', [])
            localStorage.removeItem('token')
            localStorage.removeItem('refresh')

        },
        GetInfo({commit}) {
            return new Promise(resolve => {
                getUserInfo().then(res => {
                    commit('SET_USERINFO', res.data)
                    commit('SET_PERMISSIONS', res.data.permissions)

                }).finally(() => {
                    resolve();
                })
            })
        },
        GetRoutes({commit}) {
            return new Promise(resolve => {
                getRouter().then(res => {
                    resolve(res);
                    // commit('SET_USERROUTES', res.data)
                }).finally(() => {
                    resolve();
                })
            })
        },

    },
    modules: {}
})
